using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public enum NetLockType
    {
        Rlease = 0,
        Lock = 1
    }

    /// <summary>
    /// 网络沟通锁模块
    /// 主要用于在发送消息时阻止玩家输入
    /// --转菊花
    /// </summary>
    public class NetLockModule : Singleton<NetLockModule>
    {
        /// <summary>
        /// 网络锁图像延迟显示时间(秒)
        /// </summary>
        public const float NetLockDelayShow = 1;
        /// <summary>
        /// 网络锁超时时间(秒)
        /// </summary>
        public const float NetLockTimeout = 10;

        private List<int> _sendingMsgIds = new();

        private NetLockModule()
        {
        }

        public void ClearLock()
        {
            _sendingMsgIds.Clear();
            RefreshLockingUI();
        }

        public void ClearAll()
        {
            ClearLock();
        }

        public void SetLockState(int msgId, int type)
        {
            NetLockType lockType = (NetLockType)type;
            switch (lockType)
            {
                case NetLockType.Lock:
                    _sendingMsgIds.Add(msgId);
                    break;
                case NetLockType.Rlease:
                    _sendingMsgIds.Remove(msgId);
                    break;
            }

            RefreshLockingUI();
        }

        private void RefreshLockingUI()
        {
            bool isConnecting = IsConnecting();
            if (isConnecting)
            {
                if (!ManagerCenter.UI.IsWindowShowing<UINetLock>()) //先判断，直接开可能导致刷新，这里不刷新
                {
                    ManagerCenter.UI.ShowWindowSync<UINetLock>();
                }
            }
            else
            {
                ManagerCenter.UI.CloseWindow<UINetLock>();
            }
        }

        public bool IsConnecting()
        {
            return _sendingMsgIds.Count > 0;
        }
    }
}
